package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.persistence.model.Region;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 区域表控制器
 *
 * @author fengshuonan
 * @Date 2018-01-25 00:04:42
 */
@Controller
@RequestMapping("/region")
public class RegionController extends BaseController {

    private String PREFIX = "/system/region/";

    @Autowired
    private IRegionService regionService;

    /**
     * 跳转到区域表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "region.html";
    }

    /**
     * 跳转到添加区域表
     */
    @RequestMapping("/region_add")
    public String regionAdd() {
        return PREFIX + "region_add.html";
    }

    /**
     * 跳转到修改区域表
     */
    @RequestMapping("/region_update/{regionId}")
    public String regionUpdate(@PathVariable Integer regionId, Model model) {
        Region region = regionService.selectById(regionId);
        model.addAttribute("item",region);
        LogObjectHolder.me().set(region);
        return PREFIX + "region_edit.html";
    }
    /**
     * 获取下级区域
     */
    @RequestMapping(value = "/religonByPid")
    @ResponseBody
    public Object religonByPid(String id) {
        List<Region> list=regionService.selectListByPid(id);
        return list;
    }


    /**
     * 获取区域表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return regionService.selectList(null);
    }

    /**
     * 获取区域表列表
     */
    @RequestMapping(value = "/getRegionById")
    @ResponseBody
    public Object getRegionById(String id) {
        List<Region> regionList = regionService.selectListByPid(id);//查找中国下级区域，即省份信息
        return regionList;
    }



    /**
     * 新增区域表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Region region) {
        regionService.insert(region);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除区域表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer regionId) {
        regionService.deleteById(regionId);
        return SUCCESS_TIP;
    }

    /**
     * 修改区域表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Region region) {
        regionService.updateById(region);
        return super.SUCCESS_TIP;
    }

    /**
     * 区域表详情
     */
    @RequestMapping(value = "/detail/{regionId}")
    @ResponseBody
    public Object detail(@PathVariable("regionId") Integer regionId) {
        return regionService.selectById(regionId);
    }
}
